package com.ggh.jinjilive.ui.mine.address;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CompoundButton;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.ggh.jinjilive.BaseApplication;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.ActivityAddTheGoodsBinding;
import com.ggh.jinjilive.dialog.NearMessageDialog;
import com.ggh.jinjilive.listener.AddItemOnClickListener;
import com.ggh.jinjilive.mobile.mall.goods.thegoods.TheGoodsAddressMobile;
import com.ggh.jinjilive.ui.BaseActivity;
import com.ggh.jinjilive.utils.ARouterUtils;
import com.ggh.jinjilive.utils.LocationUtils;
import com.google.gson.Gson;
import com.gyf.immersionbar.ImmersionBar;
import com.lljjcoder.Interface.OnCityItemClickListener;
import com.lljjcoder.bean.CityBean;
import com.lljjcoder.bean.DistrictBean;
import com.lljjcoder.bean.ProvinceBean;
import com.lljjcoder.style.cityjd.JDCityConfig;
import com.lljjcoder.style.cityjd.JDCityPicker;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 添加收货地址
 */

@Route(path = ARouterUtils.AddTheGoodsActivity)
public class AddTheGoodsActivity extends BaseActivity<ActivityAddTheGoodsBinding> implements View.OnClickListener {
    private TheGoodsViewModel theGoodsViewModel;

    //定位信息
    private List<PoiItem> poiItemList;
    //附近地址弹框
    private NearMessageDialog nearMessageDialog;
    //是否默认地址
    private int is_default = 0;
    //获取传输的数据
    private TheGoodsAddressMobile theGoodsAddressMobile;

    @Override
    public void initCreate() {
        super.initCreate();
        ImmersionBar.with(this).keyboardEnable(true).init();
        BaseApplication.getInstance().getAppComponent().doInjectionMain(this);
        theGoodsViewModel = new ViewModelProvider(this,viewModelFactory).get(TheGoodsViewModel.class);
    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_add_the_goods;
    }

    @Override
    public void initParam() {
        super.initParam();
        //获取传输的数据
        theGoodsAddressMobile = (TheGoodsAddressMobile) getIntent().getSerializableExtra("theGoodsAddressMobile");
    }

    @Override
    public void initView() {
        super.initView();
        setWhiteActionBarStyle("添加收货地址",true,true);
        //获取状态栏高度
        binding.llSpacing.setPadding(0, BarUtils.getStatusBarHeight(),0,0);
        //附近地址弹框
        nearMessageDialog = new NearMessageDialog(mContext);
        nearMessageDialog.setAddItemOnClickListener(new AddItemOnClickListener() {
            @Override
            public void onSkipClicked(int position, Object mObject) {
                PoiItem poiItem = (PoiItem) mObject;
                if(null != poiItem){
                    nearMessageDialog.dismiss();
                    binding.etDetailedAddress.setText(poiItem.getSnippet() + poiItem.getTitle());
                }
            }
        });
    }

    @Override
    public void initData() {
        super.initData();
        //获取传输的数据
        if(null != theGoodsAddressMobile){
            //收货人
            binding.etConsigneeName.setText(theGoodsAddressMobile.getName());
            //收货人手机号
            binding.etHandMachineNo.setText(theGoodsAddressMobile.getMobile());
            //地区
            binding.tvRegion.setText(theGoodsAddressMobile.getContent());
            //详细地址
            binding.etDetailedAddress.setText(theGoodsAddressMobile.getS_content());
            if(theGoodsAddressMobile.getIs_default() == 1){
                binding.toolSwitch.setChecked(true);
            }else {
                binding.toolSwitch.setChecked(false);
            }
        }
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        //点击选择
        binding.tvRegion.setOnClickListener(this);
        //定位
        binding.llPositioning.setOnClickListener(this);
        //保存
        binding.ButtonSave.setOnClickListener(this);
        //定位信息
        LocationUtils.positioning(new AMapLocationListener() {
            @Override
            public void onLocationChanged(AMapLocation aMapLocation) {
                LocationUtils.stopLocation();
                LatLonPoint latLonPoint = new LatLonPoint(aMapLocation.getLatitude(),aMapLocation.getLongitude());
                LocationUtils.accessoryPositioningMessage(mContext, latLonPoint, new AddItemOnClickListener() {
                    @Override
                    public void onSkipClicked(int position, Object mObject) {
                        poiItemList = (List<PoiItem>) mObject;
                    }
                });
            }
        });
        LocationUtils.startLocation();
        //设为默认地址
        binding.toolSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                if(isChecked){
                    is_default = 1;
                }else {
                    is_default = 0;
                }
            }
        });
        //地址是否添加成功
        theGoodsViewModel.address.observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                if(aBoolean){
                    finish();
                }
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tv_region://点击选择
                JDCityPicker cityPicker = new JDCityPicker();
                JDCityConfig jdCityConfig = new JDCityConfig.Builder().build();
                jdCityConfig.setShowType(JDCityConfig.ShowType.PRO_CITY_DIS);
                cityPicker.init(this);
                cityPicker.setConfig(jdCityConfig);
                cityPicker.setOnCityItemClickListener(new OnCityItemClickListener() {
                    @Override
                    public void onSelected(ProvinceBean province, CityBean city, DistrictBean district) {
                        binding.tvRegion.setText(province.getName() +"  "+ city.getName() +"  "+  district.getName());
                    }

                    @Override
                    public void onCancel() {

                    }
                });
                cityPicker.showCityPicker();
                break;
            case R.id.ll_positioning://定位
                if(null != poiItemList && poiItemList.size() > 0){
                    if(null != nearMessageDialog && !nearMessageDialog.isShowing()){
                        nearMessageDialog.show();
                        nearMessageDialog.setPoiItemList(poiItemList);
                    }
                }else {
                    ToastUtils.make().show("定位失败或者定位中");
                }
                break;
            case R.id.Button_save://保存
                //收货人
                String theConsignee = binding.etConsigneeName.getText().toString();
                if(TextUtils.isEmpty(theConsignee)){
                    ToastUtils.make().show("请输入收货人");
                    return;
                }
                //手机号码
                String cellphoneMark = binding.etHandMachineNo.getText().toString();
                if(TextUtils.isEmpty(cellphoneMark)){
                    ToastUtils.make().show("请输入手机号码");
                    return;
                }
                //地区
                String region = binding.tvRegion.getText().toString();
                if(TextUtils.isEmpty(region)){
                    ToastUtils.make().show("请选择地区");
                    return;
                }
                //详细地址
                String detailedAddress = binding.etDetailedAddress.getText().toString();
                if(TextUtils.isEmpty(detailedAddress)){
                    ToastUtils.make().show("详细地址");
                    return;
                }
                if(null != theGoodsAddressMobile){
                    Map<String, Object> map = new HashMap<>();
                    map.put("is_default",is_default);
                    map.put("name",theConsignee);
                    map.put("mobile",cellphoneMark);
                    map.put("code","012");
                    map.put("content",region);
                    map.put("s_content",detailedAddress);
                    map.put("id",theGoodsAddressMobile.getId());
                    //修改地址
                    theGoodsViewModel.editReceiveAddress(map);
                }else {
                    Map<String, Object> map = new HashMap<>();
                    map.put("is_default",is_default);
                    map.put("name",theConsignee);
                    map.put("mobile",cellphoneMark);
                    map.put("code","012");
                    map.put("content",region);
                    map.put("s_content",detailedAddress);
                    //地址添加成功
                    theGoodsViewModel.addReceiveAddress(map);
                }

                break;
        }
    }
}
